package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.AxdRiskUser;
import com.example.entity.LoginUser;
import com.example.mapper.AxdRiskUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/*
 *  @author 雨安
 *  类名： UserDetailsService
 *  创建时间：2024/3/18
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    //装配sql
    @Autowired
    AxdRiskUserMapper axdRiskUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //创建条件构造器
        LambdaQueryWrapper<AxdRiskUser> wrapper = new LambdaQueryWrapper<>();
        //设置条件
        wrapper.eq(AxdRiskUser::getUserName,username);
        //查询用户信息
        AxdRiskUser axdRiskUser = axdRiskUserMapper.selectOne(wrapper);
        //判断user是否为null判断是否查询到了用户
        if (Objects.isNull(axdRiskUser)){
            //返回null
            throw new RuntimeException("用户名或者密码错误");
        }
        //TODO 查询对应的权限信息

        //把数据封装成 UserDetails对象返回
        return new LoginUser(axdRiskUser);
    }
}
